import Bullet, { BulletState } from "./bullet";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BulletDebugDraw extends cc.Component {
    // @property(cc.Label)
    lb: cc.Label = null;

    bullet: Bullet = null;
    protected onLoad(): void {
        this.bullet = this.getComponent(Bullet) || this.addComponent(Bullet);
        this.bullet.on("state-changed", this.onStateChanged, this);


        if (!this.lb) {
            this.lb = cc.find("lb", this.node)?.getComponent(cc.Label);
        }

        if (!this.lb) {
            let node = new cc.Node("lb");
            this.lb = node.addComponent(cc.Label);
            this.lb.fontSize = 22;
            this.node.addChild(node);
        }

        this.onStateChanged(this.bullet.state);
    }

    onStateChanged(state: BulletState) {
        switch (state) {
            case BulletState.Physics:
                this.lb.string = "Physics";
                break;
            case BulletState.Grabed:
                this.lb.string = "Grabed";
                break;
        }
    }

    protected lateUpdate(dt: number): void {
        let state = "";
        switch (this.bullet.state) {
            case BulletState.Physics:
                state = "Physics";
                break;
            case BulletState.Grabed:
                state = "Grabed";
                break;
        }
        this.lb.string =
            `${state}
(${~~this.bullet.speed.x},${~~this.bullet.speed.y})${this.bullet.dockCount}
`;
    }
};